﻿using System;
using System.ComponentModel;

namespace Jiubang.Util {
	using System.Collections;
	using System.Text;

	/// <summary>
	/// 字符串处理类
	/// </summary>
	public static class StringHelper {
		/// <summary>
		/// 合并一组可枚举的对象为字符串
		/// </summary>
		/// <param name="enumerable">被枚举对象</param>
		/// <param name="seperator">分隔符</param>
		[Obsolete("Use Concat Instead")][EditorBrowsable(EditorBrowsableState.Never)]
		public static string ConcatEnumerable(this IEnumerable enumerable, string seperator) {
		    return enumerable.Concat(seperator);
		}

        /// <summary>
        /// 合并一组可枚举的对象为字符串
        /// </summary>
        /// <param name="enumerable">被枚举对象</param>
        /// <param name="seperator">分隔符</param>
        public static string Concat(this IEnumerable enumerable, string seperator) {
            IEnumerator enumerator = enumerable.GetEnumerator();
            if (!enumerator.MoveNext())
                return string.Empty;

            var text = new StringBuilder(128);
            text.Append(enumerator.Current);
            while (enumerator.MoveNext()) {
                text.Append(seperator + enumerator.Current);
            }
            return text.ToString();
        }

		/// <summary>
		/// 将以分隔符分隔的字符串转为int数组
		/// </summary>
		/// <param name="str"></param>
		/// <param name="seperator"></param>
		/// <returns></returns>
		public static int[] SplitToInt32Array(this string str, char seperator) {
			string[] items = str.Split(seperator);
			var array = new int[items.Length];
			for (int i = items.Length - 1; i >= 0; i--) {
				int value;
				if (!int.TryParse(items[i], out value))
					return null;
				array[i] = value;
			}
			return array;
		}
	}
}